package com.rui.study.micro.goods.controller;

import com.rui.study.commons.model.Result;
import com.rui.study.micro.goods.external.api.CacheService;
import com.rui.study.micro.goods.external.api.impl.RabbitMqService;
import com.rui.study.micro.goods.model.GoodsDTO;
import com.rui.study.micro.goods.service.GoodsService;
import com.rui.study.micro.goods.util.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping
public class GoodsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);


    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private RabbitMqService rabbitMqService;


    private AtomicInteger number = new AtomicInteger();


    /**
     * 新增商品种类
     * @param goodsDTO
     * @param bindingResult
     * @return
     */
    @PutMapping(path = "/goods")
    public Result addGoods(@Validated @RequestBody GoodsDTO goodsDTO,
                           BindingResult bindingResult) {
        // 新增商品
        int result = goodsService.createGoods(goodsDTO);
        return new Result<>(StatusCode.SUCCEED, result);
    }


    /**
     * 修改商品存量，指定商品id
     * @param goodsId
     * @param count
     * @return
     */
    @RequestMapping(path = "/modGoodsStock", method = RequestMethod.GET)
    public Result modGoodsStock(String goodsId, long count) {
        // 修改商品数量
        goodsService.modGoodsStock(goodsId, count);
        return new Result();
    }


    @GetMapping(value = "/value/set")
    public Result valuePut(@RequestParam("key") String key,
                           @RequestParam("value") String value) {
        cacheService.put(key, value, 60);
        return new Result();
    }

    @GetMapping(value = "/value/get")
    public Result valueGet(@RequestParam("key") String key) {
        Object value = cacheService.get(key);
        if ("000".equals(value))
            return new Result(StatusCode.ACCESS_DENIED);
        return new Result<>(StatusCode.SUCCEED, value);
    }

    @GetMapping(value = "/value/delete")
    public Result valueDelete(@RequestParam("key") String key) {
        cacheService.delete(key);
        return new Result(StatusCode.SUCCEED);
    }

//    @PostMapping(value = "/send")
//    public Result sendRabbitMq(@RequestBody MailWrapper mailWrapper) {
//        mailWrapper.setContent(number.getAndIncrement() + "-" + mailWrapper.getContent());
//        rabbitMqService.send(JsonUtil.toJson(mailWrapper));
//
//        return new Result(StatusCode.SUCCEED);
//    }

    @GetMapping(value = "/number")
    public Result number() {
        return new Result<>(StatusCode.SUCCEED, number.get());
    }

    @GetMapping(value = "/number/reset")
    public Result resetNumber() {
        return new Result<>(StatusCode.SUCCEED, number.getAndSet(1));
    }

//    @GetMapping(value = "/goods")
//    public Result<GoodsDTO> resetNumber(@RequestParam String goodsId) {
//        GoodsDTO goodsDTO = new GoodsDTO();
//        goodsDTO.setGoodsId(goodsId);
//        goodsDTO.setName("hehe");
//        return new Result<>(StatusCode.SUCCEED, goodsDTO);
//    }

//    @GetMapping(value = "/company")
//    public Result<Company> company() {
//        Company company = CompanyHelper.build();
//        LOGGER.info("Get the company \n{}", JsonUtil.encode(company));
//        return new Result<>(StatusCode.SUCCEED, company);
//    }
}
